<?php
/**
 * Created by Command.
 * User: Vito
 */

declare (strict_types = 1);

namespace app\admin\controller\v1;

use app\admin\controller\BaseController;
use app\logic\PayRecordLogic;
use app\service\Wechat\WechatH5Service;
use app\logic\UserOauthLogic;
use app\model\User;
use app\model\Timing;
class PayRecord extends BaseController
{
    protected $name = '缴费记录';

    protected $registerCurd = true;

    protected $registerCurdDir = true;
    protected $methodName = [
        'getHousing'        => '获取小区列表',
        'getTotal'          => '获取缴费总汇',
        'getLastReading'    => '获取房间的最新读数',
        'sendWorthMessage'  => '发送催缴通知',
        'getExportList'     => '获取要导出的缴费记录',
        'getWarningTime'    => '获取预警天数',
        'saveWarningTime'   => '修改预警天数',
        'cashPayProcess'    => '现金缴费流程'
    ];
    /**
     * 初始化
     */
    protected function initialize()
    {
        $this->logic = new PayRecordLogic();
    }

    /**
     * @Description: 获取全部小区
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-01 15:45:52
     * @version: 1.0.0
     * @return [type]
     */
    public function getHousing(){
        // $res = $this->logic->Lessee($params);
        $res = $this->logic->getHousing();
        if ($res) {
            return success($res, '获取成功');
        } else {
            return error('获取失败');
        }

    }
    /**
     * @Description: 获取缴费总汇
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-02 10:07:34
     * @version: 1.0.0
     * @return [type]
     */
    public function getTotal(){
        
        $params = $this->request->get();

        $res = $this->logic->getPaidTotal($params);

        if ($res) {
            return success($res, '获取成功');
        } else {
            return error('获取失败');
        }
    }

    /**
     * @Description: 获取最新的水电读数
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-16 17:53:05
     * @version: 1.0.0
     * @return [type]
     */    
    public function getLastReading(){
        $params = $this->request->get();

        $res = $this->logic->getLastReading($params);

        return success($res, '获取成功');
        /*
        if ($res) {
            return success($res, '获取成功');
        } else {
            return error('获取失败');
        }
        */
    }

    /**
     * @Description: 发送催缴通知
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-25 10:34:59
     * @version: 1.0.0
     * @return [type]
     */    
    public function sendWorthMessage(){

        $params    = $this->request->post();
        $wechat    = new WechatH5Service;
        $UserOauth = new UserOauthLogic;
        $User      = new User;
        $res = [];
        if($params['pid'] == 0){
            // 当前是对整个房间的用户都进行催收通知
            $where['pid'] = $params['id'];
            $list = $this->logic->getPidList($where);
            // $openidList = [];
            foreach ($list as $key => $value) {
                $openidWhere['user_id'] = $value['user_id'];
                $openid = $UserOauth->getOpenId($openidWhere);
                if($value['status'] == 0){
                    if ($openid) {
                        $userWhere['id']        = $value['user_id'];
                        $userInfo = $User::where($userWhere)->find();
                        $data = [
                            'touser'        => $openid,
                            'reale_name'    => $userInfo['real_name'],
                            'fee'           => $value['fee'],
                            'user_id'       => $value['user_id'],
                        ];
                        $res = $wechat->sendWorthMessage($data);
                    }
                }
                // array_push($openidList, $openid);
            }
        }else{
            //  针对个人进行崔松通知
            $where['user_id']  = $params['user_id'];
            $openid = $UserOauth->getOpenId($where);
            if(!$openid){
                return error('发送失败，该用户还未授权登录');
            }
            $data = [
                'touser'        => $openid,
                'reale_name'    => $params['room_name'],
                'fee'           => $params['fee'],
                'user_id'       => $params['user_id'],
            ];
            $res = $wechat->sendWorthMessage($data);
        }

        return success($res,'获取成功');
        
    }
    /**
     * @Description: 获取要导出的缴费记录
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-12-03 10:34:36
     * @version: 1.0.0
     * @return [type]
     */
    public function getExportList(){
        $params = $this->request->get();

        $res  = $this->logic->getExportList($params);

        return success($res,'获取成功');
    }

    /**
     * @Description: 获取预警天数
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-12-06 10:21:44
     * @version: 1.0.0
     * @return [type]
     */
    public function getWarningTime(){
        
        return success(Timing::find(1),'获取成功');
    }
    /**
     * @Description: 修改预警天数
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-12-06 10:26:30
     * @version: 1.0.0
     * @return [type]
     */
    public function saveWarningTime(){
        $params = $this->request->get();
        if($params['time']<=0){
            return error('修改失败，天数不能小于一天');
        }
        $res = Timing::where('id',1)->update(['time'=>$params['time']]);
        return success($res,'修改成功');
    }

    /**
     * @Description: 现金缴费流程
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-12-13 15:06:00
     * @version: 1.0.0
     * @return [type]
     */
    public function cashPayProcess(){
        $params = $this->request->post();
        $where['id'] = $params['id'];
        if(!$params['payees_id']){
            return error('提交失败，现金收款人为空');
        }
        $data = [
            'status'        => 1,
            'pay_type'      => 1,
            'payee'         => $params['payees_id'],
            'update_time'   => time(),
        ];
        $res = $this->logic->savePayRecord($where,$data);
        $UnWhere['pid'] = $params['pid'];
        $UnWhere['status'] = 0;
        $findRes = $this->logic->getUnpaidRecord($UnWhere);
        if(!$findRes){
            $pidWhere['id'] = $params['pid'];
            $saveData = [
                'status'        => 1,
                'update_time'   => time(),
            ];
            $saveRes = $this->logic->savePayRecord($pidWhere,$saveData);
        }
        return success($res,'修改成功');
    }
}
